// pages/details/details.js
import {request} from '../../request/index.js'
import regeneratorRuntime, { async } from '../../lib/runtime/runtime';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    dgeName: '',
    address: '',
    dgeTypeName: '',
    dgeLevel: '',
    deptName: '',
    deptId: '',
    dgeId: '',
    typeStyle: '',
    IfJudge1: true,
    JudgeForm:[], 
    steps:[], //巡检记录
    info: '', // 隐患信息
    ImgList: '', // 图片数组
    chkInfo: '', // 隐患信息数组
    chkImgList: '', // 图片
    chkForm: [], // 巡视
    showDetail: false,
    infomation: '', // 巡检单个信息
    foundname: '', // 巡检人
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
    console.log(options)
    var typeStyle = ''
    if (options.level === '一般'){
      typeStyle = 'normal'
    }
    else if(options.level ==='重要'){
      typeStyle = 'important'
    }
    else if(options.level ==='严重'){
      typeStyle = 'serious'
    }
    this.setData({
      dgeName: options.name,
      // dgeInfo: options.info,
      address: options.address,
      dgeTypeName: options.fenlei,
      dgeLevel: options.level,
      dgeId:options.Id,
      typeStyle : typeStyle
    }, () =>{
      this.getDgeInfos(options.Id)
      this.getJudgeForm(options.Id)
    })
    
  },
  async getDgeInfos(id){
    const res = await request({
      url: '/danger/findById/' + id,
      header:{
        'Authorization':'Bearer '+wx.getStorageSync('token'),
      },
      method: "get",
    })
    // console.log(res)
    if (res.data.status !== 200){
      var title = res.data.msg
      wx.showToast({
        title: title,
        icon: 'none',
        duration: 2000
      })
      if (title === '您还未登录'){
        wx.reLaunch({
          url: '/pages/index/index',
        })
      }
    }else{
      this.getDeptName(res.data.data.deptId)
      var dgeInfo = res.data.data
      if (dgeInfo) {
          var infos = (dgeInfo.dgeInfo+ "").split('imgGroup:')[0]
            var info = (infos === 'null') ? '暂无隐患信息' : infos
            var imgstr = (dgeInfo.dgeInfo+ "").split('imgGroup:')[1]
            var arr = []
            if(imgstr) {
                arr = (imgstr+ "").split(",")
            }
            else {
                arr = ["null"]
            } 
            this.setData({
              ImgList: arr,
              info: info
           })
          }
    }
  },
  // 获取所属供电所
async getDeptName(id){
  const res = await request({
    url: '/dept/showInfo/' + id,
    header:{
      'Authorization':'Bearer '+wx.getStorageSync('token'),
    },
    method: "get",
  })
  // console.log(res)
  if (res.data.status !== 200){
    var title = res.data.msg
    wx.showToast({
      title: title,
      icon: 'none',
      duration: 2000
    })
  }
  else {
    this.setData({
      deptName: res.data.data.deptName ? res.data.data.deptName : '未属于'
    })
  }
},
 // 出发去巡检
 goToCheck:function(e){
  console.log(e);
  wx.navigateTo({
    url: '/pages/sign/sign?dgeId='+ this.data.dgeId ,
  })
},

 // 获取大部分信息
 async getJudgeForm(dgeId){
  const res = await request({
    url: '/check/findByDanger/' + dgeId,
    header:{
      'Authorization':'Bearer '+wx.getStorageSync('token'),
    },
    method: "get",
  })
  // console.log(res)
  if (res.data.status !== 200){
    var title = res.data.msg
    wx.showToast({
      title: title,
      icon: 'none',
      duration: 2000
    })
    if (title === '您还未登录'){
      wx.reLaunch({
        url: '/pages/index/index',
      })
    }
  }
  else {
    var JudgeForm = res.data.data   
    var arrInfo = []
    var arr = []
    if (JudgeForm[0]) {
      JudgeForm.forEach(function(item){
        var infos = (item.chkInfo+ "").split('imgGroup:')[0]
        var info = (infos === 'null') ? '暂无巡检信息' : infos
        var imgstr = (item.chkInfo+ "").split('imgGroup:')[1]
        var arrImg = []
        if(imgstr) {
            arrImg = (imgstr+ "").split(",")
        }
        else {
            arrImg = ["null"]
        }
        arr.push({"text":item.dgeName, "id" :item.chkId, "desc": info, "time":item.chkMin,"endtime":item.updateTime})
        arrInfo.push({info:info,img:arrImg})
      });
      this.setData({
        // JudgeForm: res.data.data,
        IfJudge: false,
        steps: arr,
        chkInfo: arrInfo, 
        chkForm: JudgeForm
      })
    }
    else{
      // 没有巡查记录
      this.setData({
        IfJudge1: false
      })
    }
  }
},

step (e) {
  console.log(e)
  var index = e.detail
  console.log(this.data.chkInfo)
  console.log(this.data.chkInfo[0].img) 
  var str = this.data.chkInfo[index].img
  var infos  = this.data.chkInfo[e.detail].info
  var id = this.data.chkForm[e.detail].empId
  this.setData({
    showDetail: true,
    OneImgList: str,
    infomation: infos
  })
  this.getEmpName(id)
  console.log(str, id)
},
onClose() {
  this.setData({ showDetail: false });
},
async getEmpName(id){
  const res = await request({
    url: '/emp/showInfo/' + id,
    header:{
      'Authorization':'Bearer '+wx.getStorageSync('token'),
    },
    method: "get",
  })
  // console.log(res)
  if (res.data.status !== 200){
    var title = res.data.msg
    wx.showToast({
      title: title,
      icon: 'none',
      duration: 2000
    })
  }else{
    this.setData({
      foundname: res.data.data.empName
    })
  }
},
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})